package com.common.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.common.activity.adapter.TweetFlowImageAdapter;
import com.common.android.ui.widget.CompatibleViewPager;
import com.common.weibolib.Image;
import com.product.android.business.Const;
import com.starappallcommon.R;

/**
 * 大图预览界面，从微博模块挪过来的
 * 
 * <br>
 * Created 2014-5-16 下午12:12:29
 * 
 * @version
 * @author HuangYK
 * 
 * @see
 */
public class TweetFlowImageActivity extends StarAppBaseFramentActivity {
	private ArrayList<String> mSources = null;
	private int mPosition;
	private TweetFlowImageAdapter mAdapter;
	private CompatibleViewPager mViewPaper;
	private ImageButton mImgDelBtn;
	private TextView mImgIndexTv;
	private int mImgIndex;
	private boolean mIsShowImgIndex;
	private boolean mIsDel = false;
	private boolean isLocalImage=false;
	boolean isPraised=false;
	boolean isCanPraised=false;
	int praises=0;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		initComponent();
		initComponentValue();
		initEvent();
	}

	@Override
	protected void initComponentValue() {
		mSources = getIntent().getStringArrayListExtra(ShowBigImageConstant.KEY_URLS);
		isLocalImage=getIntent().getBooleanExtra(ShowBigImageConstant.KEY_ISLOCALIMAGE, true);
		isPraised=getIntent().getBooleanExtra(ShowBigImageConstant.KEY_ISPRAISED, false);
		isCanPraised=getIntent().getBooleanExtra(ShowBigImageConstant.KEY_ISCANPRAISED, false);
		praises=getIntent().getIntExtra(ShowBigImageConstant.KEY_PRAISES, 0);
		
		mPosition = getIntent().getIntExtra(ShowBigImageConstant.KEY_POSITION, 0);
		mIsShowImgIndex = getIntent().getBooleanExtra(Const.INTENT_KEY.IS_SHOW_IMG_INDEX, true);
		mAdapter = new TweetFlowImageAdapter(TweetFlowImageActivity.this,
				mSources,  isLocalImage,isPraised,praises,isCanPraised);
		mViewPaper.setAdapter(mAdapter);
		mViewPaper.setCurrentItem(mPosition);
		
	}

	@Override
	protected void initComponent() {
		setContentView(R.layout.flow_image_activity);
		mViewPaper = (CompatibleViewPager) findViewById(R.id.photo_pager);		
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		outState.putInt(ShowBigImageConstant.KEY_PRAISES, praises);
		outState.putInt(ShowBigImageConstant.KEY_POSITION, mPosition);
		outState.putStringArrayList(ShowBigImageConstant.KEY_URLS, mSources);
		outState.putBoolean(ShowBigImageConstant.KEY_ISLOCALIMAGE, isLocalImage);
		outState.putBoolean(ShowBigImageConstant.KEY_ISPRAISED, isPraised);
		outState.putBoolean(ShowBigImageConstant.KEY_ISCANPRAISED, isCanPraised);
		
		outState.putBoolean(Const.INTENT_KEY.IS_SHOW_IMG_INDEX, mIsShowImgIndex);
	}

	@Override
	protected void initEvent() {
		mViewPaper.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				mImgIndex = (arg0 + 1);
				if (null != mImgIndexTv) {					
					mImgIndexTv.setText(mImgIndex + "/" + mSources.size());
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int scrollState) {
			}
		});
		
		if(mIsShowImgIndex){		
			mImgIndex = mPosition + 1;
			mImgIndexTv = (TextView) findViewById(R.id.img_index);
			mImgIndexTv.setText(mImgIndex + "/" + mSources.size());
			mImgIndexTv.setVisibility(View.VISIBLE);
		}
		mImgDelBtn = (ImageButton) findViewById(R.id.img_delete);	
		if (isLocalImage) {
				mImgDelBtn.setVisibility(View.VISIBLE);
				mImgDelBtn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mIsDel = true;
						mSources.remove(mImgIndex - 1);
						if (mSources.isEmpty()) {
							finish();
						}
						mAdapter.setSources(mSources);
						mAdapter.notifyDataSetChanged();						
						if(mIsShowImgIndex){
							mImgIndex = mImgIndex > mSources.size() ? mSources
									.size() : mImgIndex;
							mImgIndexTv.setText(mImgIndex + "/" + mSources.size());
						}
						
					}
				});		
			mImgIndexTv.setText(mImgIndex + "/" + mSources.size());
		}else{
			mImgDelBtn.setVisibility(View.VISIBLE);
			mImgDelBtn.setImageResource(R.drawable.bottom_left_save);
			mImgDelBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(null != mAdapter){
						Image img =  (Image) mAdapter.getItem(mImgIndex-1);
						if(null != img){
							mAdapter.doSave(img.image_original, img.image_ext);
						}
					}					
				}
			});
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mAdapter.Destroy();
	}

	@Override
	public void finish() {
		if (mIsDel || mAdapter.mIsLike) {
			Intent intent = new Intent();
			if (mIsDel) {
				intent.putStringArrayListExtra("SELECTED_IMG", mSources);
				mIsDel = false;
			}
			if (mAdapter.mIsLike) {
				intent.putExtra("is_like", mAdapter.isTweetLike());
				mAdapter.mIsLike = false;
			}
			setResult(RESULT_OK, intent);
		}
		super.finish();
	}

}
